GBML manual / Stacks

stack_peek

Reads the item at the top of target WITHOUT removing it. If target is empty, returns a plain default value instead (see stack_pop's own note on what that is per type) - target is left unchanged either way.

Syntax

stack_peek(target) -> element

Arguments

NameType
targetstack

Returns

element

Description

Reads the item at the top of target WITHOUT removing it. target is left unchanged either way.

If target is empty, returns a plain default value instead - the same rule stack_pop uses (0/0.0/false/an empty string/nothing, depending on target's element type). Useful for deciding what to do with the top item before committing to actually removing it.

Example

/// @type {stack<int>}
var history = [1, 2, 3];
var top = stack_peek(history); // 3 - history is still [1, 2, 3]
A real capture - the stack and TOP shown as ? before the peekA real capture - the stack and TOP shown as ? before the peek
A real capture - the stack and TOP shown as ? before the peek
The same stack, unchanged - TOP now shows 3, the value stack_peek readThe same stack, unchanged - TOP now shows 3, the value stack_peek read
The same stack, unchanged - TOP now shows 3, the value stack_peek read

On the GBA

the GBA module's own vector back() - O(1), guarded by an empty() check first so it is never called on an empty vector (which would be undefined behavior, not just a wrong answer).

See also: stack_popstack_pushstack_lengthstack_emptyData containers